<template>
  <div class="dataset-home">
    <h1>LLM评测数据集管理</h1>
    
    <div class="version-info" v-if="currentVersion">
      <h3>当前数据集版本: {{ currentVersion.ver_code }}</h3>
      <div class="version-item">
        <span class="label">创建时间:</span>
        <span class="value">{{ formatDate(currentVersion.time) }}</span>
      </div>
      <div class="version-item">
        <span class="label">发布状态:</span>
        <span class="value" :class="{'published': currentVersion.published, 'unpublished': !currentVersion.published}">
          {{ currentVersion.published ? '已发布' : '未发布' }}
        </span>
      </div>
      <div class="version-description">
        <h4>版本描述</h4>
        <p>{{ currentVersion.description }}</p>
      </div>
    </div>
    <div v-else class="version-info">
      <h3>当前没有可用版本</h3>
    </div>
    <div v-if="currentVersion?.published" class="disabled-hint">
      当前版本已发布，数据导入功能已禁用
    </div>

    <div class="management-section">
      <div class="action-group">
        <div class="import-section">
          <button 
            @click="showImportSection = !showImportSection" 
            class="action-btn"
            :disabled="currentVersion?.published"
          >
            <span class="icon">⬆️</span>
            <span>导入数据</span>
          </button>
          
          <div v-if="showImportSection" class="file-inputs-container">
            <div class="file-inputs">
              <div class="file-input">
                <label>
                  <span class="icon">📄</span>
                  <span>选择原始问答文件</span>
                  <input 
                    type="file" 
                    accept=".json"
                    @change="handleOriginalFileChange"
                    style="display: none"
                    :disabled="currentVersion?.published"
                  >
                </label>
                <span v-if="originalFile">{{ originalFile.name }}</span>
              </div>
              <div class="file-input">
                <label>
                  <span class="icon">📄</span>
                  <span>选择标准问答文件</span>
                  <input 
                    type="file" 
                    accept=".json"
                    @change="handleStandardFileChange"
                    style="display: none"
                    :disabled="currentVersion?.published"
                  >
                </label>
                <span v-if="standardFile">{{ standardFile.name }}</span>
              </div>
            </div>
            <button 
              @click="importDataset" 
              class="action-btn"
              :disabled="!originalFile || !standardFile || isImporting || currentVersion?.published"
            >
              <span class="icon">⬆️</span>
              <span v-if="!isImporting">确认导入</span>
              <span v-else>导入中...</span>
            </button>
            <div v-if="importSuccess" class="import-success">
              ✅ 数据集导入成功
            </div>
            <div v-if="importError" class="import-error">
              ❌ {{ importError }}
            </div>
          </div>
        </div>
        <button 
          @click="exportDataset" 
          class="action-btn"
        >
          <span class="icon">⬇️</span>
          <span>导出数据集</span>
        </button>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, computed, ref } from 'vue'
import { useVersionStore } from '@/stores/versionStore'
import { importQADataset } from '@/api/question'

export default defineComponent({
  name: 'DatasetHome',
  setup() {
    const versionStore = useVersionStore()
    const currentVersion = computed(() => versionStore.currentVersion)

    const formatDate = (isoString: string) => {
      const date = new Date(isoString)
      return date.toLocaleDateString() + ' ' + date.toLocaleTimeString([], {hour: '2-digit', minute:'2-digit'})
    }

    const originalFile = ref<File | null>(null)
    const standardFile = ref<File | null>(null)
    const isImporting = ref(false)
    const importSuccess = ref(false)
    const importError = ref<string | null>(null)
    const showImportSection = ref(false)

    const handleOriginalFileChange = (event: Event) => {
      const input = event.target as HTMLInputElement
      if (input.files?.length) {
        originalFile.value = input.files[0]
      }
    }

    const handleStandardFileChange = (event: Event) => {
      const input = event.target as HTMLInputElement
      if (input.files?.length) {
        standardFile.value = input.files[0]
      }
    }

    const importDataset = async () => {
      if (!currentVersion.value?.ver_code) return
      
      try {
        isImporting.value = true
        importError.value = null
        
        await importQADataset(
          currentVersion.value.ver_code,
          originalFile.value as File,
          standardFile.value as File
        )
        
        importSuccess.value = true
      } catch (error: any) {
        importError.value = error.message || '导入失败'
      } finally {
        isImporting.value = false
      }
    }

    const exportDataset = () => {
      console.log('导出数据集功能')
    }

    return {
      currentVersion,
      formatDate,
      importDataset,
      exportDataset,
      handleOriginalFileChange,
      handleStandardFileChange,
      originalFile,
      standardFile,
      isImporting,
      importSuccess,
      importError,
      showImportSection
    }
  }
})
</script>

<style scoped>
.dataset-home {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
}

.version-info {
  margin: 24px 0;
  padding: 20px;
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  border: 1px solid #eaeaea;
}

.version-info h3 {
  margin-top: 0;
  margin-bottom: 16px;
  color: #1a1a1a;
  font-size: 18px;
  font-weight: 600;
}

.version-item {
  margin-bottom: 8px;
  display: flex;
}

.version-item .label {
  font-weight: bold;
  min-width: 80px;
  color: #666;
}

.version-item .value {
  color: #333;
}

.version-item .value.published {
  color: #42b983;
  font-weight: 500;
  padding: 2px 8px;
  background-color: #f0f9eb;
  border-radius: 4px;
}

.version-item .value.unpublished {
  color: #f56c6c;
  font-weight: 500;
  padding: 2px 8px;
  background-color: #fef0f0;
  border-radius: 4px;
}

.version-description {
  margin-top: 16px;
  padding: 16px;
  background-color: #f8fafc;
  border-radius: 6px;
  line-height: 1.7;
  border-left: 4px solid #409eff;
  text-align: left;
}

.version-description h4 {
  margin-top: 0;
  margin-bottom: 12px;
  color: #1a1a1a;
  font-size: 16px;
  text-align: left;
}

.version-description p {
  margin: 0;
  color: #4e5969;
  text-align: left;
}

.management-section {
  margin-top: 24px;
}

.section-title {
  margin: 8px 0;
  color: #2c3e50;
  font-size: 16px;
  font-weight: 600;
}

.action-group {
  display: flex;
  gap: 16px;
}

.action-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 20px;
  border-radius: 6px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.2s;
  background-color: #ffffff;
  border: 1px solid #e0e3e7;
}

.action-btn:hover {
  background-color: #f5f7fa;
}

.action-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.import-section {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.file-inputs-container {
  margin-top: 16px;
  padding: 16px;
  background-color: #f8f9fa;
  border-radius: 8px;
  border: 1px solid #eaeaea;
}

.file-inputs {
  display: flex;
  flex-direction: column;
  gap: 16px;
  margin-bottom: 16px;
}

.file-input {
  display: flex;
  align-items: center;
  gap: 8px;
}

.file-input label {
  display: flex;
  align-items: center;
  gap: 4px;
  cursor: pointer;
  padding: 8px 12px;
  border-radius: 4px;
  background: #f0f2f5;
}

.file-input label:hover {
  background: #e5e7eb;
}

.import-success {
  color: #42b983;
  font-size: 14px;
}

.import-error {
  color: #f56c6c;
  font-size: 14px;
}

.disabled-hint {
  color: #666;
  font-size: 12px;
  margin-top: 8px;
}

.icon {
  font-size: 16px;
}
</style>
